<template>
	<view class="scrool-page">
		<view class="list-cont">
			<scroll-view class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in detailList" :key="index">
					<uni-card :title="'序号:'+(index+1)">
						<text class="uni-body">数量：{{ item.codeEqualQty }}</text><br>
						<text class="uni-body">物料编码：{{ item.itemCode }}</text><br>
						<view class="uni-body" v-if="item.isChecked == false">
							<text class="uni-body" v-if="">是否校验：{{ "否" }}</text><br>
						</view>
						<view class="uni-body" v-else>
							<text class="uni-body">是否校验：{{ "是"}}</text><br>
						</view>
						<text class="uni-body">箱号：{{ item.boxCode }}</text><br>
					</uni-card>
				</view>
			</scroll-view>
		</view>

		<view class="footer">
			<button type="default" size='mini' @click="reduceData" style="width:32%; margin-left:1%;"
				v-if="!userflag">保留箱</button>
			<button type="default" size='mini' @click="reduceData" style="width:32%; margin-left:1%;" v-else>加箱</button>
			<button type="default" size='mini' @click="refuse" style="width:32%; margin-left:1%;">拒绝</button>
			<button type="default" size='mini' @click="submit" style="width:32%; margin-left:1%;">同意</button>
		</view>
	</view>
</template>

<script>
	import {
		getToken
	} from '@/utils/auth'
	import common from '../../pages/common/common.js'
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				detailList: [],
				sourceList: [],
				userflag: false,
				trojanCode: '',
				baseUrl: getApp().globalData.config.baseUrl,
				userName: this.$store.state.user.name,
				orgId: getApp().globalData.config.orgId,
				sourceNum: 0,
				status: '',
				title: '',
				id: '',
				index: '',
			}
		},

		onLoad: function(option) {
			this.orgId = uni.getStorageSync('orgId');
			this.trojanCode = option.pkOrder;
			this.status = option.status;
			this.title = option.title;
			this.id = option.id;
			this.index = option.index;
			uni.setNavigationBarTitle({
				title: this.trojanCode + '拆托'
			});
		},

		onHide: function() {
			this.unregisterScan();
		},

		destroyed: function() {
			this.unregisterScan();
		},

		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
				return false;
			}
			//that.unregisterScan();
			let pages = getCurrentPages(); //获取所有页面栈实例列表
			let nowPage = pages[pages.length - 1]; //当前页页面实例
			let prevPage = pages[pages.length - 2]; //上一页页面实例
			prevPage.$vm.chooseTab(that.index);
			uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
				delta: 1
			});
			return true;
		},

		onShow() {
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式				
			this.initScan();
			this.registerScan();
		},

		created: function() {
			this.orgId = uni.getStorageSync('orgId');
			this.query(this.trojanCode);
		},

		methods: {
			initScan() {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
						let codeStr = common.utf8ByteToUnicodeStr(code);
						scanDevice.stopScan(); // 停止扫描	
						var result = common.scanCode(codeStr);
						if (result[0] == "C") {
							_this.scan(result[1]);
						} else {
							uni.showToast({
								title: '请正确扫描箱号',
								icon: 'none',
								duration: 3000
							});
						}
					}
				});
			},

			scan(codeStr) {
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				var that = this;
				var tempList = [];
				if (that.userflag) { //减码
					for (var i = 0; i < that.detailList.length; i++) {
						var data = that.detailList[i];
						if (data.boxCode != codeStr) {
							tempList.push(data);
						}
					}
					that.detailList = tempList;
					uni.hideLoading();
				} else { //增箱
					var flag = false;
					for (var i = 0; i < that.detailList.length; i++) {
						var data = that.detailList[i];
						if (data.boxCode == codeStr) {
							flag = true;
							break;
						}
					}
					if (flag) {
						uni.hideLoading();
						uni.showToast({
							title: '此数据已存在',
							icon: 'none',
							duration: 3000
						});
					} else {
						that.addBox(codeStr);
					}
				}
			},

			addBox(codeStr) {
				var that = this;
				uni.request({
					url: that.baseUrl + '/system/barcode/box',
					data: {
						userName: that.userName,
						boxCode: codeStr,
						orgId: that.orgId,
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						console.log(res)
						if (res.data.code == '200' || res.data.code == 200) {
							var result = res.data.data;
							var flag = false;
							console.log(that.detailList)
							for (var i = 0; i < that.detailList.length; i++) {
								var data = that.detailList[i];
								console.log()
								console.log(data.boxCode)
								console.log(result.boxCode)
								if (data.boxCode != result.boxCode) {
									flag = true;
									break;
								}
							}
							if (flag) {
								that.detailList.push(result);
								//隐藏加载框
								uni.hideLoading();
							} else {
								//隐藏加载框
								uni.hideLoading();
								uni.showToast({
									title: '箱号为' + codeStr + '的数据不属于此托',
									icon: 'none',
									duration: 3000
								});
							}
						} else {
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return;
						}
					},
				})
			},

			registerScan() {
				main.registerReceiver(receiver, filter);
			},

			unregisterScan() {
				main.unregisterReceiver(receiver);
			},

			reduceData() {
				if (this.userflag) {
					this.userflag = false;
				} else {
					this.userflag = true;
				}
			},

			submit() {
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				// console.log(this.detailList),
				uni.request({
					url: this.baseUrl + '/system/unpline/insert',
					data: {
						userName: this.userName,
						orgId: this.orgId,
						cipherCode: this.trojanCode,
						prdList: this.detailList
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						console.log('111', res);
						console.log('222', res.data);
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							this.updateTask(res.data.msg, true);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}

					},
				})

			},

			refuse() {
				this.updateTask('', false);
			},

			//更新代办任务状态为完成
			updateTask(packageId, flag) {
				var that = this;
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.request({
					url: that.baseUrl + '/system/wmsbacklog/update',
					data: {
						id: that.id,
						executor: that.userName,
						orgId: that.orgId,
						startTime: '',
						endTime: new Date().toISOString().slice(0, 10),
						status: '3' //完成状态
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						console.log('111', res);
						console.log('222', res.data);
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							if (flag) {
								that.updateOldTask(packageId);
							} else {
								uni.showToast({
									title: '操作成功且代办任务已完成',
									icon: 'none',
									duration: 3000
								});
								//that.unregisterScan();
								setTimeout(function() {
									let pages = getCurrentPages(); //获取所有页面栈实例列表
									let nowPage = pages[pages.length - 1]; //当前页页面实例
									let prevPage = pages[pages.length - 2]; //上一页页面实例
									prevPage.$vm.chooseTab(that.index);
									uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
										delta: 1
									});
								}, 1000);
							}

						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}

					},
				})
			},

			updateOldTask(packageId) {
				var that = this;
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.request({
					url: that.baseUrl + '/system/wmsbacklog/wb2',
					data: {
						orgId: that.orgId,
						id: packageId,
						status: '1',
						userName: that.userName,
						startTime: '',
						endTime: new Date().toISOString().slice(0, 10),
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							uni.showToast({
								title: '操作成功且代办任务已完成',
								icon: 'none',
								duration: 3000
							});
							//that.unregisterScan();
							setTimeout(function() {
								let pages = getCurrentPages(); //获取所有页面栈实例列表
								let nowPage = pages[pages.length - 1]; //当前页页面实例
								let prevPage = pages[pages.length - 2]; //上一页页面实例
								prevPage.$vm.chooseTab(that.index);
								uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							}, 1000);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return;
						}

					},
				})
			},

			query() {
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/system/record/carefulBox',
					data: {
						qrcodeContent: this.trojanCode,
						orgId: this.orgId,
						userName: this.userName
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						if (res.data.code == '200' || res.data.code == 200) {
							this.detailList = res.data.data;
							//this.sourceList = res.data.data;
							this.sourceNum = this.detailList.length;
							//隐藏加载框
							uni.hideLoading();
						} else {
							//隐藏加载框
							uni.hideLoading();
							this.detailList = [];
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
						}

					},
				})
			},

		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}

	.text-content {
		font-size: 48rpx;
		text-align: center;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}

	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height: 30px;
		font-size: 26rpx;
	}

	page {
		width: 100%;
		height: 100%;
	}


	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}

	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}

	.list-cont {
		flex: 1;
		position: relative;

		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}

		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;

		}
	}


	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>